﻿using EzDesk.DesktopClient.PopupModule.Contoller;
using EzDesk.DesktopClient.PopupModule.View;
using EzDesk.DesktopClient.PopupModule.ViewContract;
using EzDesk.DesktopClient.PopupModule.ViewModel;
using EzDesk.DesktopClient.UtilityView.View;
using EzDesk.DesktopClient.UtilityView.ViewContract;
using EzDesk.EventAggregation;
using EzDesk.Modularity;
using EzDesk.DesktopClient.Event;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.PopupModule
{
    public class PopupModuleInfo : IModule
    {
        private PopupViewModel _popupViewModel;
        private NotificationPopupController _notificationPopupController;
        private IUnityContainer _moduleContainer;
        private EventSubscriptor<LogoutArgs> _logoutEventToken;


        public IModule Initialize()
        {
            _moduleContainer = IMClientUnityContainer.Instance.CreateChildContainer();
            _moduleContainer.RegisterType<IPopupView, PopupView>();


            _moduleContainer.RegisterType<INotificationPopupView, NotificationPopupView>();
            _moduleContainer.RegisterType<INotificationPopupViewModel, NotificationPopupViewModel>();

            LoginEvent.Instance.Subscribe(this.OnUserLogin);
            return this;
        }

        protected void OnUserLogin(LoginArgs args)
        {
            _notificationPopupController = new NotificationPopupController(_moduleContainer);
            _popupViewModel = new PopupViewModel(_moduleContainer,DataManagement.Instance);

            _logoutEventToken = LogoutEvent.Instance.Subscribe(this.OnUserLogout);

        }
        protected void OnUserLogout(LogoutArgs args)
        {
            _logoutEventToken.Dispose();

            if(_popupViewModel != null)
                _popupViewModel.Dispose();
            _popupViewModel = null;

            if(_notificationPopupController != null)
                _notificationPopupController.Dispose();
            _notificationPopupController = null;
        }

    }
}
